package com.fast.utils.exception;

import com.fast.utils.bean.R;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常处理
 */
@RestControllerAdvice
public class FastExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 自定义异常
     */
    @ExceptionHandler(FastException.class)
    public R handleRRException(FastException e) {
        return R.error(e.getMsg());
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public R handleDuplicateKeyException(IllegalArgumentException e) {
        logger.error(e.getMessage(), e);
        return R.error(e.getMessage());
    }

    @ExceptionHandler(AuthorizationException.class)
    public R handleAuthorizationException(AuthorizationException e) {
        logger.error(e.getMessage(), e);
        return R.error("没有权限，请联系管理员授权");
    }

    @ExceptionHandler(Exception.class)
    public R handleException(Exception e) {
        logger.error(e.getMessage(), e);
        return R.error("系统异常");
    }
}
